package team.scau.laboratory_repair.web.pc;

import com.querydsl.core.QueryResults;
import lombok.extern.apachecommons.CommonsLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import team.scau.laboratory_repair.common.base.BaseController;
import team.scau.laboratory_repair.common.json.JqGridResult;
import team.scau.laboratory_repair.modules.po.QRecordSubscription;
import team.scau.laboratory_repair.modules.po.Record;
import team.scau.laboratory_repair.modules.po.RecordSubscription;
import team.scau.laboratory_repair.service.RecordService;
import team.scau.laboratory_repair.service.RecordSubscriptionService;

/**
 * @author 30254
 * creadtedate:2018/10/22
 */
@Controller
@RequestMapping("/pc/subscribe")
@CommonsLog
public class RecordSubscriptionController extends BaseController {

    @Autowired
    private RecordService recordService;

    @Autowired
    private RecordSubscriptionService recordSubscriptionService;

    @ModelAttribute("subscribe")
    public RecordSubscription get(@RequestParam(required = false, value = "id") Long id) throws Exception {
        if (id != null && id > 0) {
            return recordSubscriptionService.findById(id);
        } else {
            return new RecordSubscription();
        }
    }

    @RequiresPermissions("biz:subscribe:list")
    @GetMapping(value = "/list", produces = "text/html;charset=utf-8")
    public String list(Model model) throws Exception{
        return "/pc/biz/subscribe/list";
    }

    @ResponseBody
    @RequiresPermissions("biz:subscribe:list")
    @RequestMapping(value = "/list.json", produces = "application/json;charset=utf-8")
    public JqGridResult list(@ModelAttribute RecordSubscription recordSubscription) throws Exception {
        QueryResults<RecordSubscription> queryResults =
                recordSubscriptionService.listOrByExample(getPage(QRecordSubscription.class), recordSubscription);
        return new JqGridResult(queryResults, queryResults.getResults());
    }

    @RequiresPermissions("biz:subscribe:view")
    @GetMapping(value = "/view", produces = "text/html;charset=utf-8")
    public String view( Record record, Model model) throws Exception{
        // TODO 图片url传出
        model.addAttribute("url","http://p4.qhimg.com/dr/100__/t01cbdfee083acb4039.png");
        return "/pc/biz/record/edit";
    }
}
